home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / BITMAP.ZIP / COLORS.C < prev    next >
C/C++ Source or Header  |  1993-04-19  |  8KB  |  355 lines

  1. /*
  2. **    $id: ssvcid colors.c 1.1 08/03/92 10:01 am$
  3. **        This file conatins the functions needed to perform the color
  4. **    modification of a Device Independant Bitmap.
  5. **
  6. **    (C) 1991-3    Larry Widing
  7. */
  8. #include    <windows.h>
  9. #include    <dos.h>
  10. #include    "bitmaps.h"
  11. #include    "bmmanip.h"
  12. #include    "colors.h"
  13.  
  14. /*
  15. **    Local function prototypes
  16. */
  17. static LPRGBQUAD    GetColors(HWND dlg);
  18. static void            ReleaseColors(HWND dlg);
  19. static int            SetColors(HWND dlg, LPRGBQUAD colors);
  20. static void            ClearColors(HWND dlg);
  21. #if    defined(__TSC__)
  22.     #pragma    save
  23.     #pragma    call(windows=>on)
  24. #endif
  25. BOOL FAR PASCAL EXPORT    ColorMapDialog(HWND, WORD, WORD, DWORD);
  26. #if    defined(__TSC__)
  27.     #pragma    restore
  28. #endif
  29.  
  30. /*
  31. **    Local variables
  32. */
  33. const char    Property[] = "Colors";
  34.  
  35. BOOL FAR PASCAL EXPORT
  36. ColorMapDialog(HWND dlg, WORD message, WORD wParam, DWORD lParam)
  37. {
  38.     int                nColors, i;
  39.     int                ok = FALSE;
  40.     HANDLE            handle;
  41.     LPRGBQUAD        colors;
  42.     LPBITMAPINFO    bmi;
  43.  
  44.     switch (message)
  45.     {
  46.         case WM_COMMAND:
  47.             switch (wParam)
  48.             {
  49.                 case IDOK:
  50.                     /*
  51.                     **    Save new color values
  52.                     */
  53.                     colors = GetColors(dlg);
  54.                     if (colors != NULL)
  55.                     {
  56.                         bmi = (LPBITMAPINFO)GlobalLock(DIBitmapHandle);
  57.                         if (bmi != NULL)
  58.                         {
  59.                             nColors = DIBitmapColors(bmi);
  60.                             for (i = 0 ; i < nColors ; ++i)
  61.                             {
  62.                                 bmi->bmiColors[i] = colors[i];
  63.                             }
  64.                             ok = TRUE;
  65.                             GlobalUnlock(DIBitmapHandle);
  66.  
  67.                             /*
  68.                             **    Update DIB Logical Palette
  69.                             */
  70.                             if (DibPalette != (HPALETTE)NULL)
  71.                             {
  72.                                 DeleteObject(DibPalette);
  73.                             }
  74.                             DibPalette = CreateDibPalette(DIBitmapHandle);
  75.                         }
  76.                     }
  77.                     EndDialog(dlg, ok);
  78.                     break;
  79.  
  80.                 case IDCANCEL:
  81.                     EndDialog(dlg, 0);
  82.                     break;
  83.  
  84.                 case CLRD_RGBLIST:
  85.                     if (HIWORD(lParam) == LBN_SELCHANGE)
  86.                     {
  87.                         /*
  88.                         **    Update the edit controls
  89.                         */
  90.                         colors = GetColors(dlg);
  91.                         if (colors != NULL)
  92.                         {
  93.                             i = (int)SendDlgItemMessage(dlg, CLRD_RGBLIST, LB_GETCURSEL, 0, 0L);
  94.                             if (i >= 0)
  95.                             {
  96.                                 SetDlgItemInt(dlg, CLRD_RED, colors[i].rgbRed, FALSE);
  97.                                 SetDlgItemInt(dlg, CLRD_GREEN, colors[i].rgbGreen, FALSE);
  98.                                 SetDlgItemInt(dlg, CLRD_BLUE, colors[i].rgbBlue, FALSE);
  99.                             }
  100.                             ReleaseColors(dlg);
  101.                         }
  102.                     }
  103.                     break;
  104.  
  105.                 case CLRD_RED:
  106.                 case CLRD_GREEN:
  107.                 case CLRD_BLUE:
  108.                     if (HIWORD(lParam) == EN_KILLFOCUS
  109.                         || HIWORD(lParam) == EN_CHANGE)
  110.                     {
  111.                         /*
  112.                         **    Update any change in this value to the listbox entry
  113.                         */
  114.                         colors = GetColors(dlg);
  115.                         if (colors != NULL)
  116.                         {
  117.                             i = (int)SendDlgItemMessage(dlg, CLRD_RGBLIST, LB_GETCURSEL, 0, 0L);
  118.                             if (i >= 0)
  119.                             {
  120.                                 if (wParam == CLRD_RED)
  121.                                     colors[i].rgbRed = GetDlgItemInt(dlg, CLRD_RED, NULL, FALSE);
  122.                                 else if (wParam == CLRD_GREEN)
  123.                                     colors[i].rgbGreen = GetDlgItemInt(dlg, CLRD_GREEN, NULL, FALSE);
  124.                                 else
  125.                                     colors[i].rgbBlue = GetDlgItemInt(dlg, CLRD_BLUE, NULL, FALSE);
  126.  
  127.                                 InvalidateRect(GetDlgItem(dlg, CLRD_RGBLIST), NULL, FALSE);
  128.                             }
  129.  
  130.                             ReleaseColors(dlg);
  131.                         }
  132.                     }
  133. //                    else if (HIWORD(lParam) == EN_CHANGE)
  134. //                    {
  135. //                        /*
  136. //                        **    Reflect any change in the color box
  137. //                        */
  138. //                    }
  139.                     break;
  140.             }
  141.             return TRUE;
  142.  
  143.         case WM_DRAWITEM:
  144.         {
  145.             LPDRAWITEMSTRUCT    draw = (LPDRAWITEMSTRUCT)lParam;
  146.             int                    item = draw->itemID;
  147.             HBRUSH                hbr;
  148.             char                    tmp[30];
  149.  
  150.             if (draw->CtlID == CLRD_RGBLIST)
  151.             {
  152.                 colors = GetColors(dlg);
  153.                 if (colors != NULL)
  154.                 {
  155.                     HPALETTE    oldPal = (HPALETTE)NULL;
  156.  
  157.                     if (DibPalette != (HPALETTE)NULL)
  158.                     {
  159.                         oldPal = SelectPalette(draw->hDC, DibPalette, TRUE);
  160.                   RealizePalette(draw->hDC);
  161.                     }
  162.  
  163.                     if (draw->itemAction == ODA_DRAWENTIRE)
  164.                     {
  165.                         /* Erase the item */
  166.                         hbr = CreateSolidBrush(RGB(colors[item].rgbRed,
  167.                             colors[item].rgbGreen, colors[item].rgbBlue));
  168.                         if (hbr != (HBRUSH)NULL)
  169.                         {
  170.                             FillRect(draw->hDC, (LPRECT)&draw->rcItem, hbr);
  171.                             DeleteObject(hbr);
  172.                         }
  173.                     
  174.                         SetTextColor(draw->hDC, RGB(255 - colors[item].rgbRed,
  175.                             255 - colors[item].rgbGreen, 255 - colors[item].rgbBlue));
  176.                         SetBkMode(draw->hDC, TRANSPARENT);
  177.                         
  178.                         wsprintf((LPSTR)tmp, (LPSTR)"%d red, %d green, %d blue",
  179.                             colors[item].rgbRed, colors[item].rgbGreen,
  180.                             colors[item].rgbBlue);
  181.  
  182.                         DrawText(draw->hDC, (LPSTR)tmp, -1, (LPRECT)&draw->rcItem,
  183.                             DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE | DT_LEFT);
  184.  
  185.                         /*
  186.                         ** Do any outlining required by being the current focus
  187.                         */
  188.                         if (draw->itemState & ODS_SELECTED)
  189.                         {
  190.                             DrawFocusRect(draw->hDC, (LPRECT)&draw->rcItem);
  191.                         }
  192.                  }
  193.                  else if (draw->itemAction == ODA_SELECT)
  194.                  {
  195.                      DrawFocusRect(draw->hDC, (LPRECT)&draw->rcItem);
  196.                  }
  197.                  
  198.                     if (oldPal != (HPALETTE)NULL)
  199.                     {
  200.                         SelectPalette(draw->hDC, oldPal, FALSE);
  201.                   RealizePalette(draw->hDC);
  202.                     }
  203.  
  204.                     ReleaseColors(dlg);
  205.                 }
  206.             }
  207.             break;
  208.         }
  209.  
  210.         case WM_MEASUREITEM:
  211.             /* Do nothing - let the default value be used */
  212.             break;
  213.  
  214.         case WM_DESTROY:
  215.             ClearColors(dlg);
  216.             return TRUE;
  217.  
  218.         case WM_CTLCOLOR:
  219.             switch (HIWORD(lParam))
  220.             {
  221.                 case CTLCOLOR_BTN:
  222.                 case CTLCOLOR_SCROLLBAR:
  223.                 case CTLCOLOR_LISTBOX:
  224.                     break;
  225.  
  226.                 case CTLCOLOR_DLG:
  227.                 case CTLCOLOR_EDIT:
  228.                 case CTLCOLOR_MSGBOX:
  229.             case CTLCOLOR_STATIC:
  230.                     SetBkColor((HDC)wParam, RGB(192,192,192));
  231.                SetTextColor((HDC)wParam, RGB(0,0,0));
  232.                 return (BOOL)(HIWORD(lParam) == CTLCOLOR_DLG ? DialogBrush : GrayBrush);
  233.             }
  234.             break;
  235.  
  236.         case WM_INITDIALOG:
  237.         {
  238.             bmi = (LPBITMAPINFO)GlobalLock(DIBitmapHandle);
  239.             if (bmi != NULL)
  240.             {
  241.                 nColors = DIBitmapColors(bmi);
  242.                 handle = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE,
  243.                     sizeof(RGBQUAD) * nColors);
  244.                 if (handle != (HANDLE)NULL)
  245.                 {
  246.                     colors = (LPRGBQUAD)GlobalLock(handle);
  247.                     if (colors != NULL)
  248.                     {
  249.                         for (i = 0 ; i < nColors ; ++i)
  250.                         {
  251.                             colors[i] = bmi->bmiColors[i];
  252.                             SendDlgItemMessage(dlg, CLRD_RGBLIST, LB_ADDSTRING, -1,
  253.                                 (LONG)i);
  254.                         }
  255.                         ok = SetColors(dlg, colors);
  256.  
  257.                         if (!ok)
  258.                         {
  259.                             GlobalUnlock(handle);
  260.                         }
  261.                     }
  262.  
  263.                     if (!ok)
  264.                     {
  265.                         GlobalFree(handle); 
  266.                     }
  267.                 }
  268.                 GlobalUnlock(DIBitmapHandle);
  269.             }
  270.  
  271.             if (!ok)
  272.             {
  273.                 ClearColors(dlg);
  274.                 EndDialog(dlg, 0);
  275.             }
  276.             break;
  277.         }
  278.     }
  279.  
  280.     return FALSE;
  281. }
  282.  
  283. /*
  284. **    Local functions for processing the Property list for storing the current
  285. **    color set.
  286. */
  287. static LPRGBQUAD
  288. GetColors(HWND dlg)
  289. {
  290.     HANDLE        handle;
  291.     LPRGBQUAD    colors = NULL;
  292.  
  293.     handle = GetProp(dlg, (LPSTR)Property);
  294.     if (handle != (HANDLE)NULL)
  295.     {
  296.         colors = (LPRGBQUAD)GlobalLock(handle);
  297.     }
  298.  
  299.     return colors;
  300. }
  301.  
  302. static void
  303. ReleaseColors(HWND dlg)
  304. {
  305.     HANDLE        handle;
  306.  
  307.     handle = GetProp(dlg, (LPSTR)Property);
  308.     if (handle != (HANDLE)NULL)
  309.     {
  310.         GlobalUnlock(handle);
  311.     }
  312. }
  313.  
  314. static int
  315. SetColors(HWND dlg, LPRGBQUAD colors)
  316. {
  317.     HANDLE    handle;
  318.     int        rc = FALSE;
  319.  
  320.     handle = (HANDLE)LOWORD(GlobalHandle(FP_SEG(colors)));
  321.     if (handle != (HANDLE)NULL)
  322.     {
  323.         ClearColors(dlg);
  324.         if (SetProp(dlg, (LPSTR)Property, handle))
  325.         {
  326.             GlobalUnlock(handle);
  327.             rc = TRUE;
  328.         }
  329.     }
  330.  
  331.     return rc;
  332. }
  333.  
  334. static void
  335. ClearColors(HWND dlg)
  336. {
  337.     HANDLE    handle;
  338.  
  339.     handle = RemoveProp(dlg, (LPSTR)Property);
  340.     if (handle != (HANDLE)NULL)
  341.     {
  342.         GlobalFree(handle);
  343.     }
  344. }
  345.  
  346. /*
  347. **    Modification History
  348. **    ====================
  349. **
  350. **    $lgb$
  351. ** 02/10/92     Larry Widing   Initial Version.
  352. ** 08/03/92     Larry Widing   
  353. **    $lge$
  354. */
  355.